function [data] = crg_wgs84_crg2html(data, file, opts)
%CRG_WGS84_CRG2HTML CRG generate html file to show wgs info in Google maps
%   CRG_WGS84_CRG2HTML(DATA, FILE, OPTS) generates html file
%   to show WGS84 coordinates (start, track, end) in Google maps
%
%   Inputs:
%   DATA    struct array as defined in CRG_INTRO
%   FILE    html file to write
%   OPTS    stuct for method options (optional, no default)
%   .mpol   maximum number of map polyline points (default: 100)
%   .minc   minimum increment of map polyline points (default: 1.0)
%   .title  title on web page (default: 'OpenCRG road overview')
%   .filenm file name on web page (default: DATA.filenm if available)
%
%   Outputs:
%   DATA    struct array as defined in CRG_INTRO
%
%   Examples:
%   data = crg_wgs84_wgs2html(data, 'crg-demo.html');
%   web('crg-demo.html', '-browser')
%   generates crg-demo.html in current directory and shows it.
%
%   See also CRG_INTRO.

%   Copyright 2005-2015 OpenCRG - Daimler AG - Jochen Rauh
%
%   Licensed under the Apache License, Version 2.0 (the "License");
%   you may not use this file except in compliance with the License.
%   You may obtain a copy of the License at
%
%       http://www.apache.org/licenses/LICENSE-2.0
%
%   Unless required by applicable law or agreed to in writing, software
%   distributed under the License is distributed on an "AS IS" BASIS,
%   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%   See the License for the specific language governing permissions and
%   limitations under the License.
%
%   More Information on OpenCRG open file formats and tools can be found at
%
%       http://www.opencrg.org
%
%   $Id: crg_wgs84_crg2html.m 358 2015-10-05 19:14:23Z jorauh@EMEA.CORPDIR.NET $

%% check if already succesfully checked

if ~isfield(data, 'ok')
    data = crg_check(data);
    if ~isfield(data, 'ok')
        error('CRG:checkError', 'check of DATA was not completely successful')
    end
end

%% handle optional args

mpol = 100;         % maximum number of polyline points
minc = 1.0;         % minimum polyline point u increment
title  = 'OpenCRG road overview';
filenm = '&lt;unknown CRG file name&gt;';

if nargin < 3
    opts = struct;
end
if isfield(opts, 'mpol')
    mpol = opts.mpol;
end
if isfield(opts, 'minc')
    minc = opts.minc;
end
if isfield(opts, 'title')
    title = opts.title;
end
if isfield(opts, 'filenm')
    filenm = opts.filenm;
else
    if isfield(data, 'filenm')
        filenm = data.filenm;
    end
end

%% check if WGS84 end is available

if ~isfield(data.head, 'eend')
    error('CRG:wgs84Error', 'WGS84 coordinates along road are missing')
end

%% generate WGS84 coordinates

npol = min(mpol, ceil((data.head.uend-data.head.ubeg)/minc));

puv = zeros(npol, 2);
puv(:, 1) = linspace(data.head.ubeg, data.head.uend, npol);

[pxy data] = crg_eval_uv2xy(data, puv);
[wgs data] = crg_wgs84_xy2wgs(data, pxy);

[pz data] = crg_eval_uv2z(data, puv([1 end],:));

%% generate map opts struct

m_opts = struct;

m_opts.title  = title;

m_opts.header = [
    '<h1>' title '</h1>' ...
    '<h3>' filenm '</h3>' ...
    ];

m_opts.height = 70;

m_opts.footer = [ ...
   '<h1 style="font-weight:normal">Open<span style="font-style:italic; color:rgb(204, 0, 0)">CRG</span><small><small' ...
   ' style="color:rgb(192, 192, 192)"><sup><sup>&reg;</sup></sup></small><span' ...
   ' style="color:rgb(102, 102, 102)">- managing the road surface ahead.</span></small>' ...
   ' </h1>' ...
   '<address>generated by '  mfilename ' at ' datestr(now, 31) '<br>' ...
   ' visit <a href="http://www.opencrg.org/">http://www.opencrg.org</a> to get latest news' ...
   ' </address>' ...
   ];

m_opts.beg_pu = [ ...
    '<h4>Start of CRG road:</h4>' ...
    '<table style=\"text-align:right\"><tbody>' ...
    '  <tr><td>U =</td><td>' num2str(data.head.ubeg, '%.3f') '</td></tr>' ...
    '  <tr><td>V =</td><td>' num2str(             0, '%.3f') '</td></tr>' ...
    '  <tr><td>X =</td><td>' num2str(data.head.xbeg, '%.3f') '</td><td></td><td>lon =</td><td>' num2str(data.head.ebeg, '%.6f') '</td></tr>' ...
    '  <tr><td>Y =</td><td>' num2str(data.head.ybeg, '%.3f') '</td><td></td><td>lat =</td><td>' num2str(data.head.nbeg, '%.6f') '</td></tr>' ...
    ];
if isfield(data.head, 'abeg')
    alti = pz(1) - data.head.zbeg + data.head.abeg;
    m_opts.beg_pu = [m_opts.beg_pu ...
        '  <tr><td>Z =</td><td>' num2str(         pz(1), '%.3f') '</td><td></td><td>alt =</td><td>' num2str(          alti, '%.6f') '</td></tr>' ...
        ];
else
    m_opts.beg_pu = [m_opts.beg_pu '<tr><td>Z =</td><td>' num2str(pz(1), '%.3f') '</td></tr>'];
end
if data.head.xoff ~= 0
    m_opts.beg_pu = [m_opts.beg_pu '<tr><td>Xoff =</td><td>' num2str(data.head.xoff, '%+.3f') '</td></tr>'];
end
if data.head.yoff ~= 0
    m_opts.beg_pu = [m_opts.beg_pu '<tr><td>Yoff =</td><td>' num2str(data.head.yoff, '%+.3f') '</td></tr>'];
end
if data.head.zoff ~= 0
    m_opts.beg_pu = [m_opts.beg_pu '<tr><td>Zoff =</td><td>' num2str(data.head.zoff, '%+.3f') '</td></tr>'];
end
m_opts.beg_pu = [m_opts.beg_pu '</tbody></table>'];

m_opts.end_pu  = [ ...
    '<h4>End of CRG road:</h4>' ...
    '<table style=\"text-align:right\"><tbody>' ...
    '  <tr><td>U =</td><td>' num2str(data.head.uend, '%.3f') '</td></tr>' ...
    '  <tr><td>V =</td><td>' num2str(             0, '%.3f') '</td></tr>' ...
    '  <tr><td>X =</td><td>' num2str(data.head.xend, '%.3f') '</td><td></td><td>lon =</td><td>' num2str(data.head.eend, '%.6f') '</td></tr>' ...
    '  <tr><td>Y =</td><td>' num2str(data.head.yend, '%.3f') '</td><td></td><td>lat =</td><td>' num2str(data.head.nend, '%.6f') '</td></tr>' ...
    ];
if isfield(data.head, 'abeg')
    alti = pz(2) - data.head.zend + data.head.aend;
    m_opts.end_pu  = [m_opts.end_pu  ...
        '  <tr><td>Z =</td><td>' num2str(         pz(2), '%.3f') '</td><td></td><td>alt =</td><td>' num2str(          alti, '%.6f') '</td></tr>' ...
        ];
else
    m_opts.end_pu  = [m_opts.end_pu  '<tr><td>Z =</td><td>' num2str(pz(2), '%.3f') '</td></tr>'];
end
if data.head.xoff ~= 0 && data.head.poff == 0
    m_opts.end_pu  = [m_opts.end_pu  '<tr><td>Xoff =</td><td>' num2str(data.head.xoff, '%+.3f') '</td></tr>'];
end
if data.head.yoff ~= 0 && data.head.poff == 0
    m_opts.end_pu  = [m_opts.end_pu  '<tr><td>Yoff =</td><td>' num2str(data.head.yoff, '%+.3f') '</td></tr>'];
end
if data.head.zoff ~= 0
    m_opts.end_pu  = [m_opts.end_pu  '<tr><td>Zoff =</td><td>' num2str(data.head.zoff, '%+.3f') '</td></tr>'];
end
m_opts.end_pu  = [m_opts.end_pu  '</tbody></table>'];


%% call map routine

map_wgs2html(wgs/180*pi, file, m_opts);

end
